//app.js
App({
  data: {
    //url: "https://webapidvp.ruemall.com/rongxun-gateway-services/",
    //url: "http://47.95.145.231:8090/rongxun-gateway-services/",
    url: "https://webapi.ruemall.com/rongxun-gateway-services/",
    //url: "http://192.168.1.57:8080/rongxun-gateway-services/",
    //reUrl: "https://dvp.rongxwy.com/base/rest/v3.1.0/"
    reUrl: "https://www.rongxwy.com/base/rest/v3.1.0/"
  },
  wxAjax: function (fun, url, bodyObj, obj) {
    var servername = url.split("/")[url.split("/").length - 1];
    var myDate = new Date();
    var timestamp = myDate.getTime();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var day = myDate.getDate();
    var hour = myDate.getHours() < 10 ? "0" + myDate.getHours() : myDate.getHours();
    var minute = myDate.getMinutes() < 10 ? "0" + myDate.getMinutes() : myDate.getMinutes();
    var second = myDate.getSeconds() < 10 ? "0" + myDate.getSeconds() : myDate.getSeconds();
    var num = Math.random();
    num = Math.ceil(num * 9999);
    var messageid = year.toString() + month.toString() + day.toString() + hour.toString() + minute.toString() + second.toString() + num;
    var headerObj = {
      "terminal": 4,
      "transaction_type": servername,
      "token": wx.getStorageSync('token'),
      "loginTime": wx.getStorageSync('loginTime'),
      "time_stamp": timestamp,
      "message_id": messageid,
      "mobile": wx.getStorageSync('userid')
    }
    var headerStr = JSON.stringify(headerObj);
    var bodyStr = JSON.stringify(bodyObj);
    var dataObj = {
      msg: '{"header":' + headerStr + ',"body":' + bodyStr + '}'
    }
    wx.request({
      url: url,
      data: dataObj,
      success: function (res) {
        fun(res, obj)
      },
      fail: function (res) {
        fun(res, obj)
      }
    })
  },
  //获取购物车数量
  getCartNum(obj) {
    var url = getApp().data.url + 'shoppingcart/getCartCount';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
      is_check: '2'
    };
    getApp().wxAjax(this.returnCartNum, url, body, obj);
  },
  returnCartNum(res, obj) {
    obj.setData({
      num: res.data.body.num
    })
  },
  //增加商品
  addPro(obj){
    console.log(obj)
    var url = getApp().data.url + 'shoppingcart/addShoppingCart';
    var body = {
      quantity: '1',
      supermarket_id: wx.getStorageSync('supermarket_id'),
      sku: obj.data.sku
    };
    
    if (obj.data.addflage == 1) {
      obj.setData({
        addflage: 2
      })
      getApp().wxAjax(this.addProSug, url, body,obj);
    }
  },
  addProSug(res,obj) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      obj.setData({
        addflage: 1
      })
      wx.showToast({
        title: '添加成功',
      })
      getApp().getCartNum(obj);
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
      obj.setData({
        addflage: 1
      })
    }
  },
  //底部导航
  bottomNav(e) {
    var kind = e.currentTarget.dataset.kind;
    var isPgae = e.currentTarget.dataset.ispage;
    var url;
    if (kind == "index" && isPgae == "1") {
      wx.redirectTo({
        url: '../eIndex/eIndex'
      })
    } else if (kind == "calss" && isPgae == "1") {
      wx.redirectTo({
        url: '../classification/classification'
      })
    } else if (kind == "cart" && isPgae == "1") {
      wx.redirectTo({
        url: '../cart/cart'
      })
    } else if (kind == "my" && isPgae == "1") {
      wx.redirectTo({
        url: '../my/my'
      })
    }
  },
})